//
//  MessageMall_Extras.m
//  funkydog
//
//  Created by Saptarshi Guha on 1/19/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "MessageMall_Extras.h"
#import "MessageCriterion.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>
#import <objc/objc-class.h>
#import "FunkyDog.h"
//So what happens,when a message is sent this is called//
//in perform search or just afett when from was selected
//1/19/08 9:23:01 PM Mail[4947] +[MessageCriterion setCriterionidentifier:from], 
// MessageCriterion: Body
//Qualifier: (null)
//Expression: nu
//= - [MessageMall mytextSearchCriterionForSearchField:8 queryString:nu  (8==entire message)
//a message is created
// MessageCriterion: Compound
//All criteria must be satisfied: Yes
//Criteria: 
//MessageCriterion: Body
//Qualifier: (null)
//Expression: nu
//MessageCriterion: Compound
//All criteria must be satisfied: Yes
//Criteria: 
//MessageCriterion: Mailbox
//Qualifier: IsNotEqualTo
//Expression: local:///Deleted%20Messages
//MessageCriterion: Mailbox
//Qualifier: IsNotEqualTo
//Expression: pop://saptarshi.guha@pop.gmail.com/Deleted%20Messages
//MessageCriterion: Mailbox
//Qualifier: IsNotEqualTo
//Expression: pop://saptarshi.guha@pop.gmail.com/Junk
//MessageCriterion: Mailbox
//Qualifier: IsNotEqualTo
//Expression: feed:///Junk
//= - [MessageMall criterionForString:nu type:8 scope:2 
//NOTE THE TYPE??==8 also note the body is empty, however it gets fixed
//then messagecriterion fix is called for each part of query
//but at one point jsut before  fix is called 
//strinfForcriteriontype:34(matddor)
/*
1/22/08 7:18:02 PM Mail[2722] v {
    AllCriteriaMustBeSatisfied = YES;
    Criteria =     (
					{
					AllCriteriaMustBeSatisfied = YES;
					Criteria =             (
											{
											CriterionUniqueId = "14da006e-06c3-4de4-9a7f-a6235bd8f7dd";
											Expression = "feed:///Junk";
											Header = Mailbox;
											Qualifier = IsNotEqualTo;
											},
											{
											CriterionUniqueId = "c78ff00e-4a07-4291-a7d7-a483b6388273";
											Expression = "pop://saptarshi.guha@pop.gmail.com/Junk";
											Header = Mailbox;
											Qualifier = IsNotEqualTo;
											},
											{
											CriterionUniqueId = "a960ed28-f16e-48a4-a339-2469b8d1cfea";
											Expression = "pop://saptarshi.guha@pop.gmail.com/Deleted%20Messages";
											Header = Mailbox;
											Qualifier = IsNotEqualTo;
											},
											{
											CriterionUniqueId = "88f9983b-2d08-45d9-b107-cb3e7d5aa68a";
											Expression = "local:///Deleted%20Messages";
											Header = Mailbox;
											Qualifier = IsNotEqualTo;
											}
											);
					CriterionUniqueId = "42675330-0b3a-4530-bced-f10c0e5eac23";
					Header = Compound;
					},
					{
					CriterionUniqueId = "162368a1-db5d-45c2-b421-0070d53b9057";
					Expression = ql;
					Header = Body;
					}
					);
    CriterionUniqueId = "da781d84-4fdb-4f7c-ba9e-1cc4eada7516";
    Header = Compound;
} 
*/
@implementation MessageMall(Extras)
+(void)load{
	if (self == [MessageMall class]){
		Method originalMethod ,replacedMethod;
		originalMethod  = class_getInstanceMethod(self, @selector(textSearchCriterionForSearchField:queryString:)); 
		replacedMethod  = class_getInstanceMethod(self, @selector(mytextSearchCriterionForSearchField:queryString:));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(criterionForString:type:scope:)); 
		replacedMethod  = class_getInstanceMethod(self, @selector(mycriterionForString:type:scope:));
		method_exchangeImplementations(originalMethod, replacedMethod);

	}
}
//Adding instance variables
//http://www.cocoabuilder.com/archive/message/cocoa/2004/12/4/123013		
- (id)mytextSearchCriterionForSearchField:(int)fp8 queryString:(id)fp12{
	if (fp8==TAGTAG)  [[FunkySob sharedInstance] setTagSearch:YES]; else  [[FunkySob sharedInstance] setTagSearch:NO];
	if (fp8==NOTETAG)  [[FunkySob sharedInstance] setNoteSearch:YES]; else  [[FunkySob sharedInstance] setNoteSearch:NO];
	if (fp8==STEROIDTAG)  [[FunkySob sharedInstance] setSteroidSearch:YES]; else  [[FunkySob sharedInstance] setSteroidSearch:NO];

	MessageCriterion *v=[self mytextSearchCriterionForSearchField:fp8 queryString:fp12];

	if(fp8==TAGTAG){
		[v setCriterionIdentifier:TAGID];
		[v setExpression:fp12];
		[v setCriterionType:1]; //formerley 1
	}
	else if(fp8==NOTETAG){
		[v setCriterionIdentifier:NOTEID];
		[v setExpression:fp12];
		[v setCriterionType:1]; //formerley 1
	}
	else if(fp8==STEROIDTAG){
		[v setCriterionIdentifier:STEROIDID];
		[v setExpression:fp12];
		[v setCriterionType:1]; //formerley 1
		//Have to convert all kMD* back to their former case.
	}
	return v;
 
}
- (id)mycriterionForString:(id)fp8 type:(int)fp12 scope:(int)fp16{
	id v=[self mycriterionForString:fp8 type:fp12 scope:fp16];
	id d=[v criteria];
	if ( [d count] >=2 && [self currentSearchField]==TAGTAG){
		MessageCriterion *o=[d objectAtIndex:1];
		[o setCriterionIdentifier:TAGID];
		[o setExpression:fp8];
		[o setCriterionType:1];
	}
	else if ( [d count] >=2 && [self currentSearchField]==NOTETAG){
		MessageCriterion *o=[d objectAtIndex:1];
		[o setCriterionIdentifier:NOTEID];
		[o setExpression:fp8];
		[o setCriterionType:1];
	}
	else if ( [d count] >=2 && [self currentSearchField]==STEROIDTAG){
		MessageCriterion *o=[d objectAtIndex:1];
		[o setCriterionIdentifier:STEROIDID];
		NSString *s=[ [FunkySob sharedInstance] fixMDQueryString:fp8];
		
		
		[o setExpression:s];
		[o setCriterionType:1];
	}
		return v;
}
@end
